<?php

require_once(DIR_LIB."VsGui".DIR_SEP."vssmartywidget.php");

require_once(DIR_LIB."Junona".DIR_SEP."junobjectsfactory.php");

class XCartOperators extends VsSmartyWidget {

	function __construct(&$page, $pager=NULL) {
		// Ne go zabravqj ina4e nqma $smarty Object
		parent::__construct();

		$this->_template = dirname(__FILE__).DIR_SEP."xcart_operators.html";

		$this->_wwwDir = 'templates/XCart/';
		$this->assign('wwwDir', $this->_wwwDir);

		//$page->addJsFile($this->_wwwDir."js/popup_image_selection.js");
		$page->addJsCode($this->jsfuncRadioValue());


		if(isset($_POST['sub_operator'])) {
			$this->procForm();
		}

		if(isset($_POST['sub_operators'])) {
			$this->procFormGroups();
		}

		if(isset($_POST['del_operators'])) {
			$this->procFormDelete();
		}

		if(isset($_POST['change_names'])) {
			//echo "change_names"; exit;
			$this->procFormChangeNames();
		}
		
		$filterGroup = isset($_REQUEST['group']) ? $_REQUEST['group'] : 0; // -1

		$operators = JunObjectsFactory::makeOperators($filterGroup);
		$this->assign('listOperators', $operators);

		$this->assign('filterGroup', $filterGroup);
		$this->assign('listGroups', JunObjectsFactory::makeOperatorGroups());

		if($pager)
			$this->assign('pager', $pager->show());
		else
			$this->assign('pager', '');

	}
	//============================================================================

	function show() {

		return $this->fetch($this->_template);
	}
	//============================================================================

	private function procForm() {
		$model = JunOperator::makeFromForm($_POST);
		$model->save();

		header("Location: ".$_SERVER['PHP_SELF']);
	}
	//===========================================================================

	private function procFormGroups() {
		foreach($_POST['op_ids'] as $id=>$value) {
			JunOperator::changeGroup($id, $_POST['posted_data'][$id]['group']);
		}

		//exit;
		header("Location: ".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']);
	}
	//===========================================================================

	private function procFormDelete() {
		//echo "EHO"; exit;
		foreach($_POST['op_ids'] as $id=>$value) {
			JunOperator::remove($id);
		}

		//exit;
		header("Location: ".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']);
	}
	//===========================================================================

	private function procFormChangeNames() {
		foreach($_POST['op_ids'] as $id=>$value) {
			JunOperator::updateNames($id, $_POST['posted_data'][$id]['name']);
		}

		header("Location: ".$_SERVER['PHP_SELF']);		// .$SERVER['HTTP_HOST']."/"
	}
	//===========================================================================

	private function jsfuncRadioValue() {
		$js = "function radioValue(radio) {";
		$js .= "	for (var i=0; i < radio.length; i++) {";
		$js .= "		if (radio[i].checked)";
		$js .= "			return radio[i].value;";
		$js .= "	}";
		$js .= "}";

		return $js;
	}
	//===========================================================================

};

?>
